<?php
include_once(dirname(__FILE__).'/../autoload.php');


class Selenium_UserManagementTest extends Lib_SeleniumTestcase
{
    protected function openUserManagement()
    {
        $this->clickButtonThatContainsText('Application');
        $this->click("css=span:contains('Manage users')");
        return $this;
    }

	public function test_ListUsers()
	{
        $this->login()->openUserManagement();
        $this->verifyTextsPresent(array('User management', 'Add user', 'Delete',
            'Save', 'Username', 'Email', 'Group', 'Status', 'Password', 'Confirm'));
        $this->verifyTextsPresent(array('Dude', 'JustAGuy', 'Jack', 'Mulder'));
    }

    public function test_editUser()
    {
        $this->login()->openUserManagement();
        $this->verifyTextPresent('JustAGuy');
        $this->fillExtEditorGridForm("css=div#userManagement", 2,
            array( 
                3 => 'just.guy@somewhere.com',
            )
        );
        $this->clickButtonThatContainsText('Save');
        $this->verifyTextPresent('User JustAGuy updated');
        $expected = array(array('email' => 'just.guy@somewhere.com'));
        $actual = Lib_QueryWrapper::runQuery(
                        "SELECT email FROM User where username = 'JustAGuy'");
        $this->assertEquals($expected, $actual);
    }

    public function test_editUser_missingEmail()
    {
        $this->login()->openUserManagement();
        $this->verifyTextPresent('JustAGuy');
        $this->fillExtEditorGridForm("css=div#userManagement", 2,
            array(
                5 => 'Passive'
            )
        );
        $this->clickExtGridCell("css=div#userManagement", 2,2);
        $this->clickButtonThatContainsText('Save');
        $this->verifyTextPresent('Unable to update user: Missing email');
    }

    public function test_editUser_changeStatus()
    {
        $this->login()->openUserManagement();
        $this->verifyTextPresent('JustAGuy');
        $this->fillExtEditorGridForm("css=div#userManagement", 2,
            array(
                3 => 'just.guy@somewhere.com',
                5 => 'Passive'
            )
        );
        $this->clickExtGridCell("css=div#userManagement", 2,2);
        $this->clickButtonThatContainsText('Save');
        $this->verifyTextPresent('User JustAGuy updated');
        $expected = array(array('userStatusId' => Core_Defines::USER_STATUS_PASSIVE));
        $actual = Lib_QueryWrapper::runQuery("SELECT userStatusId FROM User where username = 'JustAGuy'");
        $this->assertEquals($expected, $actual);
    }

    public function test_editUser_changePassword()
    {
        $newPassword = 'Qwerty123';
        $passwordHash = md5('JackQwerty123suola');

        $this->login()->openUserManagement();
        $this->verifyTextPresent('Jack');
        $this->fillExtEditorGridForm("css=div#userManagement", 3,
            array(
                3 => 'jack.bauer@nsa.com',
                6 => $newPassword,
                7 => $newPassword
            )
        );
        $this->clickExtGridCell("css=div#userManagement", 2,2);
        $this->clickButtonThatContainsText('Save');
        $this->verifyTextPresent('User Jack updated');
        $expected = array(array('password'=> $passwordHash));
        $actual = Lib_QueryWrapper::runQuery("SELECT password FROM User where username = 'Jack'");
        $this->assertEquals($expected, $actual);
    }

    public function skip_test_removeUser()
    {
        $this->login()->openUserManagement();
        $this->verifyTextPresent('Jack');
        $this->clickExtGridCell('css=div#userManagement', 3, 2);
        $this->clickButtonThatContainsText('Delete');
        $this->verifyTextPresent('Youre about to remove a user, pleace confirm.');
        $this->clickButtonThatContainsText('Yes');
        $this->verifyTextPresent('User removed');
    }
}